﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmDatCho : Form
    {
        private KhachHangService khService = new KhachHangService();
        private DatchoService dcService = new DatchoService();
        private BanService bService = new BanService();
        public int flag = 0;

        public frmDatCho()
        {
            InitializeComponent();
        }

        private Ban b = new Ban();
        private NhaHang nh = new NhaHang();
        public int maban = 0;

        public NhaHang Nh
        {
            get { return nh; }
            set { nh = value; }
        }

        public Ban B
        {
            get { return b; }
            set { b = value; }
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void LoadThongTinBan()
        {
            txtBan.Text = b.TenBan;
            if (b.TinhTrang == 0)
                cbbTinhTrang.Text = "Trống";
            if (b.TinhTrang == 1)
                cbbTinhTrang.Text = "Đã Đặt";
            if (b.TinhTrang == 2)
                cbbTinhTrang.Text = "ĐangSử Dụng";
            txtNH.Text = Nh.TenNH;
        }

        private void frmDatCho_Load(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                LoadThongTinBan();
                LoadThongTinKH();
            }
            else
            {
                LoadThongTinBan();
            }
        }

        private void LoadThongTinKH()
        {
            KhachHang kh = khService.TimKHTheoBan(b.MaBan);
            txtKH.Text = kh.TenKH;
            mtxtDT.Text = kh.SDT.ToString();
            mtxtCMND.Text = kh.CMND.ToString();
            txtSoLuong.Text = kh.Soluong.ToString();
        }

        private void btnChinhSua_Click(object sender, EventArgs e)
        {
            KhachHang kh = new KhachHang();
            kh.TenKH = txtKH.Text;
            kh.SDT = mtxtDT.Text;
            kh.CMND = mtxtCMND.Text;
            if (khService.ThemKH(kh))
            {
                kh = khService.TimKHTen(kh.TenKH,kh.CMND);
                DatCho dc = new DatCho();
                dc.MaKH = kh.MaKH;
                dc.NgayDat = DateTime.Parse(dtPDat.Text);
                dc.NgayDen = DateTime.Parse(dtPDen.Text);
                dc.TinhTrang = 1;
                dc.SoLuongNguoi = int.Parse(txtSoLuong.Text);
                dc.MaNhaHang = Nh.MaNH;
                if (dcService.ThemDatCho(dc))
                {
                    DatCho dc2 = dcService.TimChoMaKH(kh.MaKH);
                    int maDatCho = dc2.MaDatCho;
                    int maBan = B.MaBan;
                    if (dcService.ThemCTDatCho(maDatCho, maBan))
                    {
                        B.TinhTrang = 1;
                        bService.CapNhatBan(B);
                        this.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Thêm CTDC thất bại");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("Đặt chỗ thất bại");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Thêm KH thất bại");
                return;
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                KhachHang kh = khService.TimKHTen(txtKH.Text,mtxtCMND.Text);
                DatCho dc = dcService.TimChoMaKH(kh.MaKH);
                dcService.XoaDatCho(dc.MaDatCho, kh.MaKH, b.MaBan);
                this.Close();
            }
            else
                return;
        }
    }
}
